#
# Copyright (c) 2020 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Provide two classes FromJSON and TOJSON that converts between JSON and VPP's
# binary API format
#

"""
This module creates C code for core VPP, VPP plugins and client side VAT and
VAT2 tests.
"""

import datetime
import itertools
import os
import time
import sys
from io import StringIO
import shutil

process_imports = False


###############################################################################
class ToJSON:
    """Class to generate functions converting from VPP binary API to JSON."""

    _dispatch = {}
    noprint_fields = {"_vl_msg_id": None, "client_index": None, "context": None}
    is_number = {
        "u8": None,
        "i8": None,
        "u16": None,
        "i16": None,
        "u32": None,
        "i32": None,
        "u64": None,
        "i64": None,
        "f64": None,
    }

    def __init__(self, module, types, defines, imported_types, stream):
        self.stream = stream
        self.module = module
        self.defines = defines
        self.types = types
        self.types_hash = {"vl_api_" + d.name + "_t": d for d in types + imported_types}
        self.defines_hash = {d.name: d for d in defines}

    def header(self):
        """Output the top boilerplate."""
        write = self.stream.write
        write("#ifndef included_{}_api_tojson_h\n".format(self.module))
        write("#define included_{}_api_tojson_h\n".format(self.module))
        write("#include <vppinfra/cJSON.h>\n\n")
        write("#include <vlibapi/jsonformat.h>\n\n")
        if self.module == "interface_types":
            write("#define vl_printfun\n")
            write("#include <vnet/interface_types.api.h>\n\n")

    def footer(self):
        """Output the bottom boilerplate."""
        write = self.stream.write
        write("#endif\n")

    def get_base_type(self, t):
        vt_type = None
        try:
            vt = self.types_hash[t]
            if vt.type == "Using" and "length" not in vt.alias:
                vt_type = vt.alias["type"]
        except KeyError:
            vt = t
        return vt, vt_type

    def get_json_func(self, t):
        """Given the type, returns the function to use to create a
        cJSON object"""
        vt, vt_type = self.get_base_type(t)

        if t in self.is_number or vt_type in self.is_number:
            return "cJSON_AddNumberToObject", "", False
        if t == "bool":
            return "cJSON_AddBoolToObject", "", False

        # Lookup type name check if it's enum
        if vt.type == "Enum" or vt.type == "EnumFlag":
            return "{t}_tojson".format(t=t), "", True
        return "{t}_tojson".format(t=t), "&", True

    def get_json_array_func(self, t):
        """Given a type returns the function to create a cJSON object
        for arrays."""
        if t in self.is_number:
            return "cJSON_CreateNumber", ""
        if t == "bool":
            return "cJSON_CreateBool", ""
        vt, vt_type = self.get_base_type(t)
        if vt.type == "Enum" or vt.type == "EnumFlag":
            return "{t}_tojson".format(t=t), ""
        return "{t}_tojson".format(t=t), "&"

    def print_string(self, o):
        """Create cJSON object from vl_api_string_t"""
        write = self.stream.write
        if o.modern_vla:
            write(
                '    vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n'.format(
                    n=o.fieldname
                )
            )
        else:
            write(
                '    cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n'.format(
                    n=o.fieldname
                )
            )

    def print_field(self, o):
        """Called for every field in a typedef or define."""
        write = self.stream.write
        if o.fieldname in self.noprint_fields:
            return

        f, p, newobj = self.get_json_func(o.fieldtype)

        if newobj:
            write(
                '    cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n'.format(
                    f=f, p=p, n=o.fieldname
                )
            )
        else:
            write('    {f}(o, "{n}", {p}a->{n});\n'.format(f=f, p=p, n=o.fieldname))

    _dispatch["Field"] = print_field

    def print_array(self, o):
        """Converts a VPP API array to cJSON array."""
        write = self.stream.write

        forloop = """\
    {{
        int i;
        cJSON *array = cJSON_AddArrayToObject(o, "{n}");
        for (i = 0; i < {lfield}; i++) {{
            cJSON_AddItemToArray(array, {f}({p}a->{n}[i]));
        }}
    }}
"""

        if o.fieldtype == "string":
            self.print_string(o)
            return

        lfield = "a->" + o.lengthfield if o.lengthfield else o.length
        if o.fieldtype == "u8":
            write("    {\n")
            # What is length field doing here?
            write(
                '    char *s = format_c_string(0, "0x%U", format_hex_bytes_no_wrap, '
                "&a->{n}, {lfield});\n".format(n=o.fieldname, lfield=lfield)
            )
            write('    cJSON_AddStringToObject(o, "{n}", s);\n'.format(n=o.fieldname))
            write("    vec_free(s);\n")
            write("    }\n")
            return

        f, p = self.get_json_array_func(o.fieldtype)
        write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname, f=f, p=p))

    _dispatch["Array"] = print_array

    def print_enum(self, o):
        """Create cJSON object (string) for VPP API enum"""
        write = self.stream.write
        write(
            "static inline cJSON *vl_api_{name}_t_tojson "
            "(vl_api_{name}_t a) {{\n".format(name=o.name)
        )

        write("    switch(a) {\n")
        for b in o.block:
            write("    case %s:\n" % b[1])
            write('        return cJSON_CreateString("{}");\n'.format(b[0]))
        write('    default: return cJSON_CreateString("Invalid ENUM");\n')
        write("    }\n")
        write("    return 0;\n")
        write("}\n")

    _dispatch["Enum"] = print_enum

    def print_enum_flag(self, o):
        """Create cJSON object (string) for VPP API enum"""
        write = self.stream.write
        write(
            "static inline cJSON *vl_api_{name}_t_tojson "
            "(vl_api_{name}_t a) {{\n".format(name=o.name)
        )
        write("    cJSON *array = cJSON_CreateArray();\n")

        for b in o.block:
            if b[1] == 0:
                continue
            write("    if (a & {})\n".format(b[0]))
            write(
                '       cJSON_AddItemToArray(array, cJSON_CreateString("{}"));\n'.format(
                    b[0]
                )
            )
        write("    return array;\n")
        write("}\n")

    _dispatch["EnumFlag"] = print_enum_flag

    def print_typedef(self, o):
        """Create cJSON (dictionary) object from VPP API typedef"""
        write = self.stream.write
        write(
            "static inline cJSON *vl_api_{name}_t_tojson "
            "(vl_api_{name}_t *a) {{\n".format(name=o.name)
        )
        write("    cJSON *o = cJSON_CreateObject();\n")

        for t in o.block:
            self._dispatch[t.type](self, t)

        write("    return o;\n")
        write("}\n")

    def print_define(self, o):
        """Create cJSON (dictionary) object from VPP API define"""
        write = self.stream.write
        write(
            "static inline cJSON *vl_api_{name}_t_tojson "
            "(vl_api_{name}_t *a) {{\n".format(name=o.name)
        )
        write("    cJSON *o = cJSON_CreateObject();\n")
        write('    cJSON_AddStringToObject(o, "_msgname", "{}");\n'.format(o.name))
        write(
            '    cJSON_AddStringToObject(o, "_crc", "{crc:08x}");\n'.format(crc=o.crc)
        )

        for t in o.block:
            self._dispatch[t.type](self, t)

        write("    return o;\n")
        write("}\n")

    def print_using(self, o):
        """Create cJSON (dictionary) object from VPP API aliased type"""
        if o.manual_print:
            return

        write = self.stream.write
        write(
            "static inline cJSON *vl_api_{name}_t_tojson "
            "(vl_api_{name}_t *a) {{\n".format(name=o.name)
        )

        write(
            '    char *s = format_c_string(0, "%U", format_vl_api_{}_t, a);\n'.format(
                o.name
            )
        )
        write("    cJSON *o = cJSON_CreateString(s);\n")
        write("    vec_free(s);\n")
        write("    return o;\n")
        write("}\n")

    _dispatch["Typedef"] = print_typedef
    _dispatch["Define"] = print_define
    _dispatch["Using"] = print_using
    _dispatch["Union"] = print_typedef

    def generate_function(self, t):
        """Main entry point"""
        write = self.stream.write
        if t.manual_print:
            write("/* Manual print {} */\n".format(t.name))
            return
        self._dispatch[t.type](self, t)

    def generate_types(self):
        """Main entry point"""
        for t in self.types:
            self.generate_function(t)

    def generate_defines(self):
        """Main entry point"""
        for t in self.defines:
            self.generate_function(t)


class FromJSON:
    """
    Parse JSON objects into VPP API binary message structures.
    """

    _dispatch = {}
    noprint_fields = {"_vl_msg_id": None, "client_index": None, "context": None}
    is_number = {
        "u8": None,
        "i8": None,
        "u16": None,
        "i16": None,
        "u32": None,
        "i32": None,
        "u64": None,
        "i64": None,
        "f64": None,
    }

    def __init__(self, module, types, defines, imported_types, stream):
        self.stream = stream
        self.module = module
        self.defines = defines
        self.types = types
        self.types_hash = {"vl_api_" + d.name + "_t": d for d in types + imported_types}
        self.defines_hash = {d.name: d for d in defines}

    def header(self):
        """Output the top boilerplate."""
        write = self.stream.write
        write("#ifndef included_{}_api_fromjson_h\n".format(self.module))
        write("#define included_{}_api_fromjson_h\n".format(self.module))
        write("#include <vppinfra/cJSON.h>\n\n")
        write("#include <vlibapi/jsonformat.h>\n\n")
        write('#pragma GCC diagnostic ignored "-Wunused-label"\n')

    def is_base_type(self, t):
        """Check if a type is one of the VPP API base types"""
        if t in self.is_number:
            return True
        if t == "bool":
            return True
        return False

    def footer(self):
        """Output the bottom boilerplate."""
        write = self.stream.write
        write("#endif\n")

    def print_string(self, o, toplevel=False):
        """Convert JSON string to vl_api_string_t"""
        write = self.stream.write

        msgvar = "a" if toplevel else "*mp"
        msgsize = "l" if toplevel else "*len"

        if o.modern_vla:
            write("    char *p = cJSON_GetStringValue(item);\n")
            write("    size_t plen = strlen(p);\n")
            write(
                "    {msgvar} = cJSON_realloc({msgvar}, {msgsize} + plen);\n".format(
                    msgvar=msgvar, msgsize=msgsize
                )
            )
            write("    if ({msgvar} == 0) goto error;\n".format(msgvar=msgvar))
            write(
                "    vl_api_c_string_to_api_string(p, (void *){msgvar} + "
                "{msgsize} - sizeof(vl_api_string_t));\n".format(
                    msgvar=msgvar, msgsize=msgsize
                )
            )
            write("    {msgsize} += plen;\n".format(msgsize=msgsize))
        else:
            write(
                "    strncpy_s((char *)a->{n}, sizeof(a->{n}), "
                "cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n".format(
                    n=o.fieldname
                )
            )

    def print_field(self, o, toplevel=False):
        """Called for every field in a typedef or define."""
        write = self.stream.write
        if o.fieldname in self.noprint_fields:
            return
        is_bt = self.is_base_type(o.fieldtype)
        t = "vl_api_{}".format(o.fieldtype) if is_bt else o.fieldtype

        msgvar = "(void **)&a" if toplevel else "mp"
        msgsize = "&l" if toplevel else "len"

        if is_bt:
            write(
                "    vl_api_{t}_fromjson(item, &a->{n});\n".format(
                    t=o.fieldtype, n=o.fieldname
                )
            )
        else:
            write(
                "    if ({t}_fromjson({msgvar}, "
                "{msgsize}, item, &a->{n}) < 0) goto error;\n".format(
                    t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize
                )
            )

    _dispatch["Field"] = print_field

    def print_array(self, o, toplevel=False):
        """Convert JSON array to VPP API array"""
        write = self.stream.write

        forloop = """\
    {{
        int i;
        cJSON *array = cJSON_GetObjectItem(o, "{n}");
        int size = cJSON_GetArraySize(array);
        if (size != {lfield}) goto error;
        for (i = 0; i < size; i++) {{
            cJSON *e = cJSON_GetArrayItem(array, i);
            {call}
        }}
    }}
"""
        forloop_vla = """\
    {{
        int i;
        cJSON *array = cJSON_GetObjectItem(o, "{n}");
        int size = cJSON_GetArraySize(array);
        {lfield} = size;
        {realloc} = cJSON_realloc({realloc}, {msgsize} + sizeof({t}) * size);
        {t} *d = (void *){realloc} + {msgsize};
        {msgsize} += sizeof({t}) * size;
        for (i = 0; i < size; i++) {{
            cJSON *e = cJSON_GetArrayItem(array, i);
            {call}
        }}
    }}
"""
        t = o.fieldtype
        if o.fieldtype == "string":
            self.print_string(o, toplevel)
            return

        lfield = "a->" + o.lengthfield if o.lengthfield else o.length
        msgvar = "(void **)&a" if toplevel else "mp"
        realloc = "a" if toplevel else "*mp"
        msgsize = "l" if toplevel else "*len"

        if o.fieldtype == "u8":
            if o.lengthfield:
                write('    s = u8string_fromjson(o, "{}");\n'.format(o.fieldname))
                write("    if (!s) goto error;\n")
                write("    {} = vec_len(s);\n".format(lfield))

                write(
                    "    {realloc} = cJSON_realloc({realloc}, {msgsize} + "
                    "vec_len(s));\n".format(
                        msgvar=msgvar, msgsize=msgsize, realloc=realloc
                    )
                )
                write(
                    "    clib_memcpy((void *){realloc} + {msgsize}, s, "
                    "vec_len(s));\n".format(realloc=realloc, msgsize=msgsize)
                )
                write("    {msgsize} += vec_len(s);\n".format(msgsize=msgsize))

                write("    vec_free(s);\n")
            else:
                write(
                    '    if (u8string_fromjson2(o, "{n}", a->{n}) < 0) goto error;\n'.format(
                        n=o.fieldname
                    )
                )
            return

        is_bt = self.is_base_type(o.fieldtype)

        if o.lengthfield:
            if is_bt:
                call = "vl_api_{t}_fromjson(e, &d[i]);".format(t=o.fieldtype)
            else:
                call = "if ({t}_fromjson({msgvar}, len, e, &d[i]) < 0) goto error; ".format(
                    t=o.fieldtype, msgvar=msgvar
                )
            write(
                forloop_vla.format(
                    lfield=lfield,
                    t=o.fieldtype,
                    n=o.fieldname,
                    call=call,
                    realloc=realloc,
                    msgsize=msgsize,
                )
            )
        else:
            if is_bt:
                call = "vl_api_{t}_fromjson(e, &a->{n}[i]);".format(t=t, n=o.fieldname)
            else:
                call = "if ({}_fromjson({}, len, e, &a->{}[i]) < 0) goto error;".format(
                    t, msgvar, o.fieldname
                )
            write(
                forloop.format(
                    lfield=lfield,
                    t=t,
                    n=o.fieldname,
                    call=call,
                    msgvar=msgvar,
                    realloc=realloc,
                    msgsize=msgsize,
                )
            )

    _dispatch["Array"] = print_array

    def print_enum(self, o):
        """Convert to JSON enum(string) to VPP API enum (int)"""
        write = self.stream.write
        write(
            "static inline int vl_api_{n}_t_fromjson"
            "(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n".format(n=o.name)
        )
        write("    char *p = cJSON_GetStringValue(o);\n")
        for b in o.block:
            write(
                '    if (strcmp(p, "{}") == 0) {{*a = {}; return 0;}}\n'.format(
                    b[0], b[1]
                )
            )
        write("    *a = 0;\n")
        write("    return -1;\n")
        write("}\n")

    _dispatch["Enum"] = print_enum

    def print_enum_flag(self, o):
        """Convert to JSON enum(string) to VPP API enum (int)"""
        write = self.stream.write
        write(
            "static inline int vl_api_{n}_t_fromjson "
            "(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n".format(n=o.name)
        )
        write("   int i;\n")
        write("   *a = 0;\n")
        write("   for (i = 0; i < cJSON_GetArraySize(o); i++) {\n")
        write("       cJSON *e = cJSON_GetArrayItem(o, i);\n")
        write("       char *p = cJSON_GetStringValue(e);\n")
        write("       if (!p) return -1;\n")
        for b in o.block:
            write('       if (strcmp(p, "{}") == 0) *a |= {};\n'.format(b[0], b[1]))
        write("    }\n")
        write("   return 0;\n")
        write("}\n")

    _dispatch["EnumFlag"] = print_enum_flag

    def print_typedef(self, o):
        """Convert from JSON object to VPP API binary representation"""
        write = self.stream.write

        write(
            "static inline int vl_api_{name}_t_fromjson (void **mp, "
            "int *len, cJSON *o, vl_api_{name}_t *a) {{\n".format(name=o.name)
        )
        write("    cJSON *item __attribute__ ((unused));\n")
        write("    u8 *s __attribute__ ((unused));\n")
        for t in o.block:
            if t.type == "Field" and t.is_lengthfield:
                continue
            write('\n    item = cJSON_GetObjectItem(o, "{}");\n'.format(t.fieldname))
            write("    if (!item) goto error;\n")
            self._dispatch[t.type](self, t)

        write("\n    return 0;\n")
        write("\n  error:\n")
        write("    return -1;\n")
        write("}\n")

    def print_union(self, o):
        """Convert JSON object to VPP API binary union"""
        write = self.stream.write

        write(
            "static inline int vl_api_{name}_t_fromjson (void **mp, "
            "int *len, cJSON *o, vl_api_{name}_t *a) {{\n".format(name=o.name)
        )
        write("    cJSON *item __attribute__ ((unused));\n")
        write("    u8 *s __attribute__ ((unused));\n")
        for t in o.block:
            if t.type == "Field" and t.is_lengthfield:
                continue
            write('    item = cJSON_GetObjectItem(o, "{}");\n'.format(t.fieldname))
            write("    if (item) {\n")
            self._dispatch[t.type](self, t)
            write("    };\n")
        write("\n    return 0;\n")
        write("\n  error:\n")
        write("    return -1;\n")
        write("}\n")

    def print_define(self, o):
        """Convert JSON object to VPP API message"""
        write = self.stream.write
        error = 0
        write(
            "static inline vl_api_{name}_t *vl_api_{name}_t_fromjson "
            "(cJSON *o, int *len) {{\n".format(name=o.name)
        )
        write("    cJSON *item __attribute__ ((unused));\n")
        write("    u8 *s __attribute__ ((unused));\n")
        write("    int l = sizeof(vl_api_{}_t);\n".format(o.name))
        write("    vl_api_{}_t *a = cJSON_malloc(l);\n".format(o.name))
        write("\n")

        for t in o.block:
            if t.fieldname in self.noprint_fields:
                continue
            if t.type == "Field" and t.is_lengthfield:
                continue
            write('    item = cJSON_GetObjectItem(o, "{}");\n'.format(t.fieldname))
            write("    if (!item) goto error;\n")
            error += 1
            self._dispatch[t.type](self, t, toplevel=True)
            write("\n")

        write("    *len = l;\n")
        write("    return a;\n")

        if error:
            write("\n  error:\n")
            write("    cJSON_free(a);\n")
            write("    return 0;\n")
        write("}\n")

    def print_using(self, o):
        """Convert JSON field to VPP type alias"""
        write = self.stream.write

        if o.manual_print:
            return

        t = o.using
        write(
            "static inline int vl_api_{name}_t_fromjson (void **mp, "
            "int *len, cJSON *o, vl_api_{name}_t *a) {{\n".format(name=o.name)
        )
        if "length" in o.alias:
            if t.fieldtype != "u8":
                raise ValueError(
                    "Error in processing type {} for {}".format(t.fieldtype, o.name)
                )
            write(
                "    vl_api_u8_string_fromjson(o, (u8 *)a, {});\n".format(
                    o.alias["length"]
                )
            )
        else:
            write("    vl_api_{t}_fromjson(o, ({t} *)a);\n".format(t=t.fieldtype))

        write("    return 0;\n")
        write("}\n")

    _dispatch["Typedef"] = print_typedef
    _dispatch["Define"] = print_define
    _dispatch["Using"] = print_using
    _dispatch["Union"] = print_union

    def generate_function(self, t):
        """Main entry point"""
        write = self.stream.write
        if t.manual_print:
            write("/* Manual print {} */\n".format(t.name))
            return
        self._dispatch[t.type](self, t)

    def generate_types(self):
        """Main entry point"""
        for t in self.types:
            self.generate_function(t)

    def generate_defines(self):
        """Main entry point"""
        for t in self.defines:
            self.generate_function(t)


def generate_tojson(s, modulename, stream):
    """Generate all functions to convert from API to JSON"""
    write = stream.write

    write("/* Imported API files */\n")
    for i in s["Import"]:
        f = i.filename.replace("plugins/", "")
        write("#include <{}_tojson.h>\n".format(f))

    pp = ToJSON(modulename, s["types"], s["Define"], s["imported"]["types"], stream)
    pp.header()
    pp.generate_types()
    pp.generate_defines()
    pp.footer()
    return ""


def generate_fromjson(s, modulename, stream):
    """Generate all functions to convert from JSON to API"""
    write = stream.write
    write("/* Imported API files */\n")
    for i in s["Import"]:
        f = i.filename.replace("plugins/", "")
        write("#include <{}_fromjson.h>\n".format(f))

    pp = FromJSON(modulename, s["types"], s["Define"], s["imported"]["types"], stream)
    pp.header()
    pp.generate_types()
    pp.generate_defines()
    pp.footer()

    return ""


###############################################################################


DATESTRING = datetime.datetime.utcfromtimestamp(
    int(os.environ.get("SOURCE_DATE_EPOCH", time.time()))
)
TOP_BOILERPLATE = """\
/*
 * VLIB API definitions {datestring}
 * Input file: {input_filename}
 * Automatically generated: please edit the input file NOT this file!
 */

#include <stdbool.h>
#if defined(vl_msg_id)||defined(vl_union_id) \\
    || defined(vl_printfun) ||defined(vl_endianfun) \\
    || defined(vl_api_version)||defined(vl_typedefs) \\
    || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
    || defined(vl_api_version_tuple) || defined(vl_calcsizefun)
/* ok, something was selected */
#else
#warning no content included from {input_filename}
#endif

#define VL_API_PACKED(x) x __attribute__ ((packed))

/*
 * Note: VL_API_MAX_ARRAY_SIZE is set to an arbitrarily large limit.
 *
 * However, any message with a ~2 billion element array is likely to break the
 * api handling long before this limit causes array element endian issues.
 *
 * Applications should be written to create reasonable api messages.
 */
#define VL_API_MAX_ARRAY_SIZE 0x7fffffff

"""

BOTTOM_BOILERPLATE = """\
/****** API CRC (whole file) *****/

#ifdef vl_api_version
vl_api_version({input_filename}, {file_crc:#08x})

#endif
"""


def msg_ids(s):
    """Generate macro to map API message id to handler"""
    output = """\

/****** Message ID / handler enum ******/

#ifdef vl_msg_id
"""

    for t in s["Define"]:
        output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % (
            t.name.upper(),
            t.name,
        )
    output += "#endif"

    return output


def msg_names(s):
    """Generate calls to name mapping macro"""
    output = """\

/****** Message names ******/

#ifdef vl_msg_name
"""

    for t in s["Define"]:
        dont_trace = 0 if t.dont_trace else 1
        output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
    output += "#endif"

    return output


def msg_name_crc_list(s, suffix):
    """Generate list of names to CRC mappings"""
    output = """\

/****** Message name, crc list ******/

#ifdef vl_msg_name_crc_list
"""
    output += "#define foreach_vl_msg_name_crc_%s " % suffix

    for t in s["Define"]:
        output += "\\\n_(VL_API_%s, %s, %08x) " % (t.name.upper(), t.name, t.crc)
    output += "\n#endif"

    return output


def api2c(fieldtype):
    """Map between API type names and internal VPP type names"""
    mappingtable = {
        "string": "vl_api_string_t",
    }
    if fieldtype in mappingtable:
        return mappingtable[fieldtype]
    return fieldtype


def typedefs(filename):
    """Include in the main files to the types file"""
    output = """\

/****** Typedefs ******/

#ifdef vl_typedefs
#include "{include}.api_types.h"
#endif
""".format(
        include=filename
    )
    return output


FORMAT_STRINGS = {
    "u8": "%u",
    "bool": "%u",
    "i8": "%d",
    "u16": "%u",
    "i16": "%d",
    "u32": "%u",
    "i32": "%ld",
    "u64": "%llu",
    "i64": "%lld",
    "f64": "%.2f",
}


class Printfun:
    """Functions for pretty printing VPP API messages"""

    _dispatch = {}
    noprint_fields = {"_vl_msg_id": None, "client_index": None, "context": None}

    def __init__(self, stream):
        self.stream = stream

    @staticmethod
    def print_string(o, stream):
        """Pretty print a vl_api_string_t"""
        write = stream.write
        if o.modern_vla:
            write("    if (vl_api_string_len(&a->{f}) > 0) {{\n".format(f=o.fieldname))
            write(
                '        s = format(s, "\\n%U{f}: %U", '
                "format_white_space, indent, "
                "vl_api_format_string, (&a->{f}));\n".format(f=o.fieldname)
            )
            write("    } else {\n")
            write(
                '        s = format(s, "\\n%U{f}:", '
                "format_white_space, indent);\n".format(f=o.fieldname)
            )
            write("    }\n")
        else:
            write(
                '    s = format(s, "\\n%U{f}: %s", '
                "format_white_space, indent, a->{f});\n".format(f=o.fieldname)
            )

    def print_field(self, o, stream):
        """Pretty print API field"""
        write = stream.write
        if o.fieldname in self.noprint_fields:
            return
        if o.fieldtype in FORMAT_STRINGS:
            f = FORMAT_STRINGS[o.fieldtype]
            write(
                '    s = format(s, "\\n%U{n}: {f}", '
                "format_white_space, indent, a->{n});\n".format(n=o.fieldname, f=f)
            )
        else:
            write(
                '    s = format(s, "\\n%U{n}: %U", '
                "format_white_space, indent, "
                "format_{t}, &a->{n}, indent);\n".format(n=o.fieldname, t=o.fieldtype)
            )

    _dispatch["Field"] = print_field

    def print_array(self, o, stream):
        """Pretty print API array"""
        write = stream.write

        forloop = """\
    for (i = 0; i < {lfield}; i++) {{
        s = format(s, "\\n%U{n}: %U",
                   format_white_space, indent, format_{t}, &a->{n}[i], indent);
    }}
"""

        forloop_format = """\
    for (i = 0; i < {lfield}; i++) {{
        s = format(s, "\\n%U{n}: {t}",
                   format_white_space, indent, a->{n}[i]);
    }}
"""

        if o.fieldtype == "string":
            self.print_string(o, stream)
            return

        if o.fieldtype == "u8":
            if o.lengthfield:
                write(
                    '    s = format(s, "\\n%U{n}: %U", format_white_space, '
                    "indent, format_hex_bytes, a->{n}, a->{lfield});\n".format(
                        n=o.fieldname, lfield=o.lengthfield
                    )
                )
            else:
                write(
                    '    s = format(s, "\\n%U{n}: %U", format_white_space, '
                    "indent, format_hex_bytes, a, {lfield});\n".format(
                        n=o.fieldname, lfield=o.length
                    )
                )
            return

        lfield = "a->" + o.lengthfield if o.lengthfield else o.length
        if o.fieldtype in FORMAT_STRINGS:
            write(
                forloop_format.format(
                    lfield=lfield, t=FORMAT_STRINGS[o.fieldtype], n=o.fieldname
                )
            )
        else:
            write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))

    _dispatch["Array"] = print_array

    @staticmethod
    def print_alias(k, v, stream):
        """Pretty print type alias"""
        write = stream.write
        if "length" in v.alias and v.alias["length"] and v.alias["type"] == "u8":
            write(
                '    return format(s, "%U", format_hex_bytes, a, {});\n'.format(
                    v.alias["length"]
                )
            )
        elif v.alias["type"] in FORMAT_STRINGS:
            write(
                '    return format(s, "{}", *a);\n'.format(
                    FORMAT_STRINGS[v.alias["type"]]
                )
            )
        else:
            write('    return format(s, "{} (print not implemented)");\n'.format(k))

    @staticmethod
    def print_enum(o, stream):
        """Pretty print API enum"""
        write = stream.write
        write("    switch(*a) {\n")
        for b in o:
            write("    case %s:\n" % b[1])
            write('        return format(s, "{}");\n'.format(b[0]))
        write("    }\n")

    _dispatch["Enum"] = print_enum
    _dispatch["EnumFlag"] = print_enum

    def print_obj(self, o, stream):
        """Entry point"""
        write = stream.write

        if o.type in self._dispatch:
            self._dispatch[o.type](self, o, stream)
        else:
            write(
                '    s = format(s, "\\n{} {} {} (print not implemented");\n'.format(
                    o.type, o.fieldtype, o.fieldname
                )
            )


def printfun(objs, stream, modulename):
    """Main entry point for pretty print function generation"""
    write = stream.write

    h = """\
/****** Print functions *****/
#ifdef vl_printfun
#ifndef included_{module}_printfun
#define included_{module}_printfun

#ifdef LP64
#define _uword_fmt \"%lld\"
#define _uword_cast (long long)
#else
#define _uword_fmt \"%ld\"
#define _uword_cast long
#endif

#include "{module}.api_tojson.h"
#include "{module}.api_fromjson.h"

"""

    signature = """\
static inline u8 *vl_api_{name}_t_format (u8 *s,  va_list *args)
{{
    __attribute__((unused)) vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
    u32 indent __attribute__((unused)) = 2;
    int i __attribute__((unused));
"""

    h = h.format(module=modulename)
    write(h)

    pp = Printfun(stream)
    for t in objs:
        if t.manual_print:
            write("/***** manual: vl_api_%s_t_format *****/\n\n" % t.name)
            continue
        write(signature.format(name=t.name, suffix=""))
        write("    /* Message definition: vl_api_{}_t: */\n".format(t.name))
        write('    s = format(s, "vl_api_%s_t:");\n' % t.name)
        for o in t.block:
            pp.print_obj(o, stream)
        write("    return s;\n")
        write("}\n\n")

    write("\n#endif")
    write("\n#endif /* vl_printfun */\n")

    return ""


def printfun_types(objs, stream, modulename):
    """Pretty print API types"""
    write = stream.write
    pp = Printfun(stream)

    h = """\
/****** Print functions *****/
#ifdef vl_printfun
#ifndef included_{module}_printfun_types
#define included_{module}_printfun_types

"""
    h = h.format(module=modulename)
    write(h)

    signature = """\
static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
{{
    vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
    u32 indent __attribute__((unused)) = va_arg (*args, u32);
    int i __attribute__((unused));
    indent += 2;
"""

    for t in objs:
        if t.__class__.__name__ == "Enum" or t.__class__.__name__ == "EnumFlag":
            write(signature.format(name=t.name))
            pp.print_enum(t.block, stream)
            write("    return s;\n")
            write("}\n\n")
            continue

        if t.manual_print:
            write("/***** manual: vl_api_%s_t_format *****/\n\n" % t.name)
            continue

        if t.__class__.__name__ == "Using":
            write(signature.format(name=t.name))
            pp.print_alias(t.name, t, stream)
            write("}\n\n")
            continue

        write(signature.format(name=t.name))
        for o in t.block:
            pp.print_obj(o, stream)

        write("    return s;\n")
        write("}\n\n")

    write("\n#endif")
    write("\n#endif /* vl_printfun_types */\n")


def generate_imports(imports):
    """Add #include matching the API import statements"""
    output = "/* Imported API files */\n"
    output += "#ifndef vl_api_version\n"

    for i in imports:
        s = i.filename.replace("plugins/", "")
        output += "#include <{}.h>\n".format(s)
    output += "#endif\n"
    return output


ENDIAN_STRINGS = {
    "u16": "clib_net_to_host_u16",
    "u32": "clib_net_to_host_u32",
    "u64": "clib_net_to_host_u64",
    "i16": "clib_net_to_host_i16",
    "i32": "clib_net_to_host_i32",
    "i64": "clib_net_to_host_i64",
    "f64": "clib_net_to_host_f64",
}


def get_endian_string(fieldtype):
    """Return proper endian string conversion function"""
    return ENDIAN_STRINGS[fieldtype]


def get_lengthfield_type(fieldname, block):
    """Return the type of the length field"""
    for o in block:
        if o.fieldname == fieldname:
            return o.fieldtype
    return None


def endianfun_array(o, block):
    """Generate endian functions for arrays"""
    forloop = """\
    ASSERT((u32){length} <= (u32)VL_API_MAX_ARRAY_SIZE);
    for (i = 0; i < {length}; i++) {{
        a->{name}[i] = {format}(a->{name}[i]);
    }}
"""

    forloop_format = """\
    for (i = 0; i < {length}; i++) {{
        {type}_endian(&a->{name}[i], to_net);
    }}
"""

    output = ""
    if o.fieldtype == "u8" or o.fieldtype == "string" or o.fieldtype == "bool":
        output += "    /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname)
    else:
        # lfield = "a->" + o.lengthfield if o.lengthfield else o.length
        if o.lengthfield:
            fieldtype = get_lengthfield_type(o.lengthfield, block)
            if fieldtype == "u8":
                output += f"    u32 count = a->{o.lengthfield};\n"
            else:
                output += (
                    f"    u32 count = to_net ? {get_endian_string(fieldtype)}(a->{o.lengthfield}) : "
                    f"a->{o.lengthfield};\n"
                )
            lfield = "count"
        else:
            lfield = o.length

        if o.fieldtype in ENDIAN_STRINGS:
            output += forloop.format(
                length=lfield,
                format=get_endian_string(o.fieldtype),
                name=o.fieldname,
            )
        else:
            output += forloop_format.format(
                length=lfield, type=o.fieldtype, name=o.fieldname
            )
    return output


NO_ENDIAN_CONVERSION = {"client_index": None}


def endianfun_obj(o, block):
    """Generate endian conversion function for type"""
    output = ""
    if o.type == "Array":
        return endianfun_array(o, block)
    if o.type != "Field":
        output += '    s = format(s, "\\n{} {} {} (print not implemented");\n'.format(
            o.type, o.fieldtype, o.fieldname
        )
        return output
    if o.fieldname in NO_ENDIAN_CONVERSION:
        output += "    /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname)
        return output
    if o.fieldtype in ENDIAN_STRINGS:
        output += "    a->{name} = {format}(a->{name});\n".format(
            name=o.fieldname, format=get_endian_string(o.fieldtype)
        )
    elif o.fieldtype.startswith("vl_api_"):
        output += "    {type}_endian(&a->{name}, to_net);\n".format(
            type=o.fieldtype, name=o.fieldname
        )
    else:
        output += "    /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname)

    return output


def endianfun(objs, modulename):
    """Main entry point for endian function generation"""
    output = """\

/****** Endian swap functions *****/\n\
#ifdef vl_endianfun
#ifndef included_{module}_endianfun
#define included_{module}_endianfun

#undef clib_net_to_host_uword
#undef clib_host_to_net_uword
#ifdef LP64
#define clib_net_to_host_uword clib_net_to_host_u64
#define clib_host_to_net_uword clib_host_to_net_u64
#else
#define clib_net_to_host_uword clib_net_to_host_u32
#define clib_host_to_net_uword clib_host_to_net_u32
#endif

"""
    output = output.format(module=modulename)

    signature = """\
static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a, bool to_net)
{{
    int i __attribute__((unused));
"""

    for t in objs:
        if t.__class__.__name__ == "Enum" or t.__class__.__name__ == "EnumFlag":
            output += signature.format(name=t.name)
            if t.enumtype in ENDIAN_STRINGS:
                output += "    *a = {}(*a);\n".format(get_endian_string(t.enumtype))
            else:
                output += "    /* a->{name} = a->{name} (no-op) */\n".format(
                    name=t.name
                )

            output += "}\n\n"
            continue

        if t.manual_endian:
            output += "/***** manual: vl_api_%s_t_endian  *****/\n\n" % t.name
            continue

        if t.__class__.__name__ == "Using":
            output += signature.format(name=t.name)
            if "length" in t.alias and t.alias["length"] and t.alias["type"] == "u8":
                output += "    /* a->{name} = a->{name} (no-op) */\n".format(
                    name=t.name
                )
            elif t.alias["type"] in FORMAT_STRINGS:
                output += "    *a = {}(*a);\n".format(
                    get_endian_string(t.alias["type"])
                )
            else:
                output += "    /* Not Implemented yet {} */".format(t.name)
            output += "}\n\n"
            continue

        output += signature.format(name=t.name)

        for o in t.block:
            output += endianfun_obj(o, t.block)
        output += "}\n\n"

    output += "\n#endif"
    output += "\n#endif /* vl_endianfun */\n\n"

    return output


def calc_size_fun(objs, modulename):
    """Main entry point for calculate size function generation"""
    output = """\

/****** Calculate size functions *****/\n\
#ifdef vl_calcsizefun
#ifndef included_{module}_calcsizefun
#define included_{module}_calcsizefun

"""
    output = output.format(module=modulename)

    signature = """\
/* calculate message size of message in network byte order */
static inline uword vl_api_{name}_t_calc_size (vl_api_{name}_t *a)
{{
"""

    for o in objs:
        tname = o.__class__.__name__

        output += signature.format(name=o.name)
        output += f"      return sizeof(*a)"
        if tname == "Using":
            if "length" in o.alias:
                try:
                    tmp = int(o.alias["length"])
                    if tmp == 0:
                        raise (f"Unexpected length '0' for alias {o}")
                except:
                    # output += f" + vl_api_{o.alias.name}_t_calc_size({o.name})"
                    print("culprit:")
                    print(o)
                    print(dir(o.alias))
                    print(o.alias)
                    raise
        elif tname == "Enum" or tname == "EnumFlag":
            pass
        else:
            for b in o.block:
                if b.type == "Option":
                    continue
                elif b.type == "Field":
                    if b.fieldtype.startswith("vl_api_"):
                        output += f" - sizeof(a->{b.fieldname})"
                        output += f" + {b.fieldtype}_calc_size(&a->{b.fieldname})"
                elif b.type == "Array":
                    if b.lengthfield:
                        m = list(
                            filter(lambda x: x.fieldname == b.lengthfield, o.block)
                        )
                        if len(m) != 1:
                            raise Exception(
                                f"Expected 1 match for field '{b.lengthfield}', got '{m}'"
                            )
                        lf = m[0]
                        if lf.fieldtype in ENDIAN_STRINGS:
                            output += f" + {get_endian_string(lf.fieldtype)}(a->{b.lengthfield}) * sizeof(a->{b.fieldname}[0])"
                        elif lf.fieldtype == "u8":
                            output += (
                                f" + a->{b.lengthfield} * sizeof(a->{b.fieldname}[0])"
                            )
                        else:
                            raise Exception(
                                f"Don't know how to endian swap {lf.fieldtype}"
                            )
                    else:
                        # Fixed length strings decay to nul terminated u8
                        if b.fieldtype == "string":
                            if b.modern_vla:
                                output += f" + vl_api_string_len(&a->{b.fieldname})"

        output += ";\n"
        output += "}\n\n"
    output += "\n#endif"
    output += "\n#endif /* vl_calcsizefun */\n\n"

    return output


def version_tuple(s, module):
    """Generate semantic version string"""
    output = """\
/****** Version tuple *****/

#ifdef vl_api_version_tuple

"""
    if "version" in s["Option"]:
        v = s["Option"]["version"]
        (major, minor, patch) = v.split(".")
        output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % (
            module,
            major,
            minor,
            patch,
        )

    output += "\n#endif /* vl_api_version_tuple */\n\n"

    return output


def generate_include_enum(s, module, stream):
    """Generate <name>.api_enum.h"""
    write = stream.write

    if "Define" in s:
        write("typedef enum {\n")
        for t in s["Define"]:
            write("   VL_API_{},\n".format(t.name.upper()))
        write("   VL_MSG_{}_LAST\n".format(module.upper()))
        write("}} vl_api_{}_enum_t;\n".format(module))


def generate_include_counters(s, stream):
    """Include file for the counter data model types."""
    write = stream.write

    for counters in s:
        csetname = counters.name
        write("typedef enum {\n")
        for c in counters.block:
            write("   {}_ERROR_{},\n".format(csetname.upper(), c["name"].upper()))
        write("   {}_N_ERROR\n".format(csetname.upper()))
        write("}} vl_counter_{}_enum_t;\n".format(csetname))

        write("extern vlib_error_desc_t {}_error_counters[];\n".format(csetname))


def generate_include_types(s, module, stream):
    """Generate separate API _types file."""
    write = stream.write

    write("#ifndef included_{module}_api_types_h\n".format(module=module))
    write("#define included_{module}_api_types_h\n".format(module=module))

    if "version" in s["Option"]:
        v = s["Option"]["version"]
        (major, minor, patch) = v.split(".")
        write(
            "#define VL_API_{m}_API_VERSION_MAJOR {v}\n".format(
                m=module.upper(), v=major
            )
        )
        write(
            "#define VL_API_{m}_API_VERSION_MINOR {v}\n".format(
                m=module.upper(), v=minor
            )
        )
        write(
            "#define VL_API_{m}_API_VERSION_PATCH {v}\n".format(
                m=module.upper(), v=patch
            )
        )

    if "Import" in s:
        write("/* Imported API files */\n")
        for i in s["Import"]:
            filename = i.filename.replace("plugins/", "")
            write("#include <{}_types.h>\n".format(filename))

    for o in itertools.chain(s["types"], s["Define"]):
        tname = o.__class__.__name__
        if tname == "Using":
            if "length" in o.alias:
                write(
                    "typedef %s vl_api_%s_t[%s];\n"
                    % (o.alias["type"], o.name, o.alias["length"])
                )
            else:
                write("typedef %s vl_api_%s_t;\n" % (o.alias["type"], o.name))
        elif tname == "Enum" or tname == "EnumFlag":
            if o.enumtype == "u32":
                write("typedef enum {\n")
            else:
                write("typedef enum __attribute__((packed)) {\n")

            for b in o.block:
                write("    %s = %s,\n" % (b[0], b[1]))
            write("} vl_api_%s_t;\n" % o.name)
            if o.enumtype != "u32":
                size1 = "sizeof(vl_api_%s_t)" % o.name
                size2 = "sizeof(%s)" % o.enumtype
                err_str = "size of API enum %s is wrong" % o.name
                write('STATIC_ASSERT(%s == %s, "%s");\n' % (size1, size2, err_str))
        else:
            if tname == "Union":
                write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" % o.name)
            else:
                write(
                    ("typedef struct __attribute__ ((packed)) _vl_api_%s {\n") % o.name
                )
            for b in o.block:
                if b.type == "Option":
                    continue
                if b.type == "Field":
                    write("    %s %s;\n" % (api2c(b.fieldtype), b.fieldname))
                elif b.type == "Array":
                    if b.lengthfield:
                        write("    %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname))
                    else:
                        # Fixed length strings decay to nul terminated u8
                        if b.fieldtype == "string":
                            if b.modern_vla:
                                write(
                                    "    {} {};\n".format(
                                        api2c(b.fieldtype), b.fieldname
                                    )
                                )
                            else:
                                write("    u8 {}[{}];\n".format(b.fieldname, b.length))
                        else:
                            write(
                                "    %s %s[%s];\n"
                                % (api2c(b.fieldtype), b.fieldname, b.length)
                            )
                else:
                    raise ValueError(
                        "Error in processing type {} for {}".format(b, o.name)
                    )

            write("} vl_api_%s_t;\n" % o.name)
            write(
                f"#define VL_API_{o.name.upper()}_IS_CONSTANT_SIZE ({0 if o.vla else 1})\n\n"
            )

    for t in s["Define"]:
        write(
            '#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'.format(
                n=t.name, ID=t.name.upper(), crc=t.crc
            )
        )

    write("\n#endif\n")


def generate_c_boilerplate(services, defines, counters, file_crc, module, stream):
    """VPP side plugin."""
    write = stream.write
    define_hash = {d.name: d for d in defines}

    hdr = """\
#define vl_endianfun		/* define message structures */
#include "{module}.api.h"
#undef vl_endianfun

#define vl_calcsizefun
#include "{module}.api.h"
#undef vl_calsizefun

/* instantiate all the print functions we know about */
#define vl_printfun
#include "{module}.api.h"
#undef vl_printfun

#include "{module}.api_json.h"
"""

    write(hdr.format(module=module))
    if len(defines) > 0:
        write("static u16\n")
        write("setup_message_id_table (void) {\n")
        write("   api_main_t *am = my_api_main;\n")
        write("   vl_msg_api_msg_config_t c;\n")
        write(
            '   u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", '
            "VL_MSG_{m}_LAST);\n".format(module, crc=file_crc, m=module.upper())
        )
        write(f"   vec_add1(am->json_api_repr, (u8 *)json_api_repr_{module});\n")

    for d in defines:
        write(
            '   vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
            "                                VL_API_{ID} + msg_id_base);\n".format(
                n=d.name, ID=d.name.upper(), crc=d.crc
            )
        )
    for s in services:
        d = define_hash[s.caller]
        write(
            "   c = (vl_msg_api_msg_config_t) "
            " {{.id = VL_API_{ID} + msg_id_base,\n"
            '   .name = "{n}",\n'
            "   .handler = vl_api_{n}_t_handler,\n"
            "   .endian = vl_api_{n}_t_endian,\n"
            "   .format_fn = vl_api_{n}_t_format,\n"
            "   .traced = 1,\n"
            "   .replay = 1,\n"
            "   .tojson = vl_api_{n}_t_tojson,\n"
            "   .fromjson = vl_api_{n}_t_fromjson,\n"
            "   .calc_size = vl_api_{n}_t_calc_size,\n"
            "   .is_autoendian = {auto}}};\n".format(
                n=s.caller, ID=s.caller.upper(), auto=d.autoendian
            )
        )
        write("   vl_msg_api_config (&c);\n")
        try:
            d = define_hash[s.reply]
            write(
                "   c = (vl_msg_api_msg_config_t) "
                "{{.id = VL_API_{ID} + msg_id_base,\n"
                '  .name = "{n}",\n'
                "  .handler = 0,\n"
                "  .endian = vl_api_{n}_t_endian,\n"
                "  .format_fn = vl_api_{n}_t_format,\n"
                "  .traced = 1,\n"
                "  .replay = 1,\n"
                "  .tojson = vl_api_{n}_t_tojson,\n"
                "  .fromjson = vl_api_{n}_t_fromjson,\n"
                "  .calc_size = vl_api_{n}_t_calc_size,\n"
                "  .is_autoendian = {auto}}};\n".format(
                    n=s.reply, ID=s.reply.upper(), auto=d.autoendian
                )
            )
            write("   vl_msg_api_config (&c);\n")
        except KeyError:
            pass

        try:
            if s.stream:
                d = define_hash[s.stream_message]
                write(
                    "   c = (vl_msg_api_msg_config_t) "
                    "{{.id = VL_API_{ID} + msg_id_base,\n"
                    '  .name = "{n}",\n'
                    "  .handler = 0,\n"
                    "  .endian = vl_api_{n}_t_endian,\n"
                    "  .format_fn = vl_api_{n}_t_format,\n"
                    "  .traced = 1,\n"
                    "  .replay = 1,\n"
                    "  .tojson = vl_api_{n}_t_tojson,\n"
                    "  .fromjson = vl_api_{n}_t_fromjson,\n"
                    "  .calc_size = vl_api_{n}_t_calc_size,\n"
                    "  .is_autoendian = {auto}}};\n".format(
                        n=s.stream_message,
                        ID=s.stream_message.upper(),
                        auto=d.autoendian,
                    )
                )
                write("   vl_msg_api_config (&c);\n")
        except KeyError:
            pass
    if len(defines) > 0:
        write("   return msg_id_base;\n")
        write("}\n")

    severity = {
        "error": "VL_COUNTER_SEVERITY_ERROR",
        "info": "VL_COUNTER_SEVERITY_INFO",
        "warn": "VL_COUNTER_SEVERITY_WARN",
    }

    for cnt in counters:
        csetname = cnt.name
        write("vlib_error_desc_t {}_error_counters[] = {{\n".format(csetname))
        for c in cnt.block:
            write("  {\n")
            write('   .name = "{}",\n'.format(c["name"]))
            write('   .desc = "{}",\n'.format(c["description"]))
            write("   .severity = {},\n".format(severity[c["severity"]]))
            write("  },\n")
        write("};\n")


def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, stream):
    """Generate code for legacy style VAT. To be deleted."""
    write = stream.write

    define_hash = {d.name: d for d in defines}

    hdr = """\
#define vl_endianfun            /* define message structures */
#include "{module}.api.h"
#undef vl_endianfun

#define vl_calcsizefun
#include "{module}.api.h"
#undef vl_calsizefun

/* instantiate all the print functions we know about */
#define vl_printfun
#include "{module}.api.h"
#undef vl_printfun

"""

    write(hdr.format(module=module))
    for s in services:
        try:
            d = define_hash[s.reply]
        except KeyError:
            continue
        if d.manual_print:
            write(
                "/*\n"
                " * Manual definition requested for: \n"
                " * vl_api_{n}_t_handler()\n"
                " */\n".format(n=s.reply)
            )
            continue
        if not define_hash[s.caller].autoreply:
            write(
                "/* Generation not supported (vl_api_{n}_t_handler()) */\n".format(
                    n=s.reply
                )
            )
            continue
        write("#ifndef VL_API_{n}_T_HANDLER\n".format(n=s.reply.upper()))
        write("static void\n")
        write("vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n".format(n=s.reply))
        write("   vat_main_t * vam = {}_test_main.vat_main;\n".format(module))
        write("   i32 retval = ntohl(mp->retval);\n")
        write("   if (vam->async_mode) {\n")
        write("      vam->async_errors += (retval < 0);\n")
        write("   } else {\n")
        write("      vam->retval = retval;\n")
        write("      vam->result_ready = 1;\n")
        write("   }\n")
        write("}\n")
        write("#endif\n")

        for e in s.events:
            if define_hash[e].manual_print:
                continue
            write("static void\n")
            write("vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n".format(n=e))
            write('    vlib_cli_output(0, "{n} event called:");\n'.format(n=e))
            write(
                '    vlib_cli_output(0, "%U", vl_api_{n}_t_format, mp);\n'.format(n=e)
            )
            write("}\n")

    write("static void\n")
    write("setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n")
    for s in services:
        write(
            "   vl_msg_api_config (&(vl_msg_api_msg_config_t){{\n"
            "    .id = VL_API_{ID} + msg_id_base,\n"
            '    .name = "{n}",\n'
            "    .handler = vl_api_{n}_t_handler,\n"
            "    .endian = vl_api_{n}_t_endian,\n"
            "    .format_fn = vl_api_{n}_t_format,\n"
            "    .size = sizeof(vl_api_{n}_t),\n"
            "    .traced = 1,\n"
            "    .tojson = vl_api_{n}_t_tojson,\n"
            "    .fromjson = vl_api_{n}_t_fromjson,\n"
            "    .calc_size = vl_api_{n}_t_calc_size,\n"
            "   }});".format(n=s.reply, ID=s.reply.upper())
        )
        write(
            '   hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'.format(
                n=s.caller
            )
        )
        try:
            write(
                '   hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'.format(
                    n=s.caller, help=define_hash[s.caller].options["vat_help"]
                )
            )
        except KeyError:
            pass

        # Events
        for e in s.events:
            write(
                "   vl_msg_api_config (&(vl_msg_api_msg_config_t){{\n"
                "    .id = VL_API_{ID} + msg_id_base,\n"
                '    .name = "{n}",\n'
                "    .handler = vl_api_{n}_t_handler,\n"
                "    .endian = vl_api_{n}_t_endian,\n"
                "    .format_fn = vl_api_{n}_t_format,\n"
                "    .size = sizeof(vl_api_{n}_t),\n"
                "    .traced = 1,\n"
                "    .tojson = vl_api_{n}_t_tojson,\n"
                "    .fromjson = vl_api_{n}_t_fromjson,\n"
                "    .calc_size = vl_api_{n}_t_calc_size,\n"
                "   }});".format(n=e, ID=e.upper())
            )

    write("}\n")
    write("clib_error_t * vat_plugin_register (vat_main_t *vam)\n")
    write("{\n")
    write("   {n}_test_main_t * mainp = &{n}_test_main;\n".format(n=module))
    write("   mainp->vat_main = vam;\n")
    write(
        "   mainp->msg_id_base = vl_client_get_first_plugin_msg_id "
        '                       ("{n}_{crc:08x}");\n'.format(n=module, crc=file_crc)
    )
    write("   if (mainp->msg_id_base == (u16) ~0)\n")
    write(
        '      return clib_error_return (0, "{} plugin not loaded...");\n'.format(
            module
        )
    )
    write("   setup_message_id_table (vam, mainp->msg_id_base);\n")
    write("#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n")
    write("    VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n")
    write("#endif\n")
    write("   return 0;\n")
    write("}\n")


def apifunc(func):
    """Check if a method is generated already."""

    def _f(module, d, processed, *args):
        if d.name in processed:
            return None
        processed[d.name] = True
        return func(module, d, *args)

    return _f


def c_test_api_service(s, dump, stream):
    """Generate JSON code for a service."""
    write = stream.write

    req_reply_template = """\
static cJSON *
api_{n} (cJSON *o)
{{
  vl_api_{n}_t *mp;
  int len;
  if (!o) return 0;
  mp = vl_api_{n}_t_fromjson(o, &len);
  if (!mp) {{
    fprintf(stderr, "Failed converting JSON to API\\n");
    return 0;
  }}

  mp->_vl_msg_id = vac_get_msg_index(VL_API_{N}_CRC);
  vl_api_{n}_t_endian(mp, 1);
  vac_write((char *)mp, len);
  cJSON_free(mp);

  /* Read reply */
  char *p;
  int l;
  vac_read(&p, &l, 5); // XXX: Fix timeout
  if (p == 0 || l == 0) return 0;
    // XXX Will fail in case of event received. Do loop
  if (ntohs(*((u16 *)p)) != vac_get_msg_index(VL_API_{R}_CRC)) {{
    fprintf(stderr, "Mismatched reply\\n");
    return 0;
  }}
  vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
  vl_api_{r}_t_endian(rmp, 0);
  return vl_api_{r}_t_tojson(rmp);
}}

"""
    dump_details_template = """\
static cJSON *
api_{n} (cJSON *o)
{{
  u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
  int len;
  if (!o) return 0;
  vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
  if (!mp) {{
      fprintf(stderr, "Failed converting JSON to API\\n");
      return 0;
  }}
  mp->_vl_msg_id = msg_id;
  vl_api_{n}_t_endian(mp, 1);
  vac_write((char *)mp, len);
  cJSON_free(mp);

  vat2_control_ping(123); // FIX CONTEXT
  cJSON *reply = cJSON_CreateArray();

  u16 ping_reply_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_REPLY_CRC);
  u16 details_msg_id = vac_get_msg_index(VL_API_{R}_CRC);

  while (1) {{
    /* Read reply */
    char *p;
    int l;
    vac_read(&p, &l, 5); // XXX: Fix timeout
    if (p == 0 || l == 0) {{
      cJSON_free(reply);
      return 0;
    }}

    /* Message can be one of [_details, control_ping_reply
     * or unrelated event]
     */
    u16 reply_msg_id = ntohs(*((u16 *)p));
    if (reply_msg_id == ping_reply_msg_id) {{
        break;
    }}

    if (reply_msg_id == details_msg_id) {{
        if (l < sizeof(vl_api_{r}_t)) {{
            cJSON_free(reply);
            return 0;
        }}
        vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
        vl_api_{r}_t_endian(rmp, 0);
        cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
    }}
  }}
  return reply;
}}

"""
    gets_details_reply_template = """\
static cJSON *
api_{n} (cJSON *o)
{{
    u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
  int len = 0;
  if (!o) return 0;
  vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
  if (!mp) {{
    fprintf(stderr, "Failed converting JSON to API\\n");
    return 0;
  }}
  mp->_vl_msg_id = msg_id;

  vl_api_{n}_t_endian(mp, 1);
  vac_write((char *)mp, len);
  cJSON_free(mp);

  cJSON *reply = cJSON_CreateArray();

  u16 reply_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
  u16 details_msg_id = vac_get_msg_index(VL_API_{D}_CRC);

  while (1) {{
    /* Read reply */
    char *p;
    int l;
    vac_read(&p, &l, 5); // XXX: Fix timeout

    /* Message can be one of [_details, control_ping_reply
     * or unrelated event]
     */
    u16 msg_id = ntohs(*((u16 *)p));
    if (msg_id == reply_msg_id) {{
        vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
        vl_api_{r}_t_endian(rmp, 0);
        cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
        break;
    }}

    if (msg_id == details_msg_id) {{
        vl_api_{d}_t *rmp = (vl_api_{d}_t *)p;
        vl_api_{d}_t_endian(rmp, 0);
        cJSON_AddItemToArray(reply, vl_api_{d}_t_tojson(rmp));
    }}
  }}
  return reply;
}}

"""

    if dump:
        if s.stream_message:
            write(
                gets_details_reply_template.format(
                    n=s.caller,
                    r=s.reply,
                    N=s.caller.upper(),
                    R=s.reply.upper(),
                    d=s.stream_message,
                    D=s.stream_message.upper(),
                )
            )
        else:
            write(
                dump_details_template.format(
                    n=s.caller, r=s.reply, N=s.caller.upper(), R=s.reply.upper()
                )
            )
    else:
        write(
            req_reply_template.format(
                n=s.caller, r=s.reply, N=s.caller.upper(), R=s.reply.upper()
            )
        )


def generate_c_test2_boilerplate(services, defines, module, stream):
    """Generate code for VAT2 plugin."""
    write = stream.write

    define_hash = {d.name: d for d in defines}
    # replies = {}

    hdr = """\
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vppinfra/error.h>
#include <vnet/ip/ip_format_fns.h>
#include <vnet/ethernet/ethernet_format_fns.h>

#define vl_typedefs             /* define message structures */
#include <vlibmemory/vl_memory_api_h.h>
#include <vlibmemory/vlib.api_types.h>
#include <vlibmemory/vlib.api.h>
#undef vl_typedefs

#include "{module}.api_enum.h"
#include "{module}.api_types.h"

#define vl_endianfun		/* define message structures */
#include "{module}.api.h"
#undef vl_endianfun

#define vl_calcsizefun
#include "{module}.api.h"
#undef vl_calsizefun

#define vl_printfun
#include "{module}.api.h"
#undef vl_printfun

#include "{module}.api_tojson.h"
#include "{module}.api_fromjson.h"
#include <vpp-api/client/vppapiclient.h>

#include <vat2/vat2_helpers.h>

"""

    write(hdr.format(module=module))

    for s in services:
        if s.reply not in define_hash:
            continue
        c_test_api_service(s, s.stream, stream)

    write(
        "void vat2_register_function(char *, cJSON * (*)(cJSON *), cJSON * (*)(void *), u32);\n"
    )
    # write('__attribute__((constructor))')
    write("clib_error_t *\n")
    write("vat2_register_plugin (void) {\n")
    for s in services:
        if s.reply not in define_hash:
            continue
        crc = define_hash[s.caller].crc
        write(
            '   vat2_register_function("{n}", api_{n}, (cJSON * (*)(void *))vl_api_{n}_t_tojson, 0x{crc:08x});\n'.format(
                n=s.caller, crc=crc
            )
        )
    write("   return 0;\n")
    write("}\n")


#
# Plugin entry point
#
def run(output_dir, apifilename, s):
    """Main plugin entry point."""
    stream = StringIO()

    if not output_dir:
        sys.stderr.write("Missing --outputdir argument")
        return None

    basename = os.path.basename(apifilename)
    filename, _ = os.path.splitext(basename)
    modulename = filename.replace(".", "_")
    filename_enum = os.path.join(output_dir + "/" + basename + "_enum.h")
    filename_types = os.path.join(output_dir + "/" + basename + "_types.h")
    filename_c = os.path.join(output_dir + "/" + basename + ".c")
    filename_c_test = os.path.join(output_dir + "/" + basename + "_test.c")
    filename_c_test2 = os.path.join(output_dir + "/" + basename + "_test2.c")
    filename_c_tojson = os.path.join(output_dir + "/" + basename + "_tojson.h")
    filename_c_fromjson = os.path.join(output_dir + "/" + basename + "_fromjson.h")

    # Generate separate types file
    st = StringIO()
    generate_include_types(s, modulename, st)
    with open(filename_types, "w") as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Generate separate enum file
    st = StringIO()
    st.write("#ifndef included_{}_api_enum_h\n".format(modulename))
    st.write("#define included_{}_api_enum_h\n".format(modulename))
    generate_include_enum(s, modulename, st)
    generate_include_counters(s["Counters"], st)
    st.write("#endif\n")
    with open(filename_enum, "w") as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Generate separate C file
    st = StringIO()
    generate_c_boilerplate(
        s["Service"], s["Define"], s["Counters"], s["file_crc"], modulename, st
    )
    with open(filename_c, "w") as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Generate separate C test file
    st = StringIO()
    plugin = bool("plugin" in apifilename)
    generate_c_test_boilerplate(
        s["Service"], s["Define"], s["file_crc"], modulename, plugin, st
    )
    with open(filename_c_test, "w") as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Fully autogenerated VATv2 C test file
    st = StringIO()
    generate_c_test2_boilerplate(s["Service"], s["Define"], modulename, st)
    with open(filename_c_test2, "w") as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()  #

    # Generate separate JSON file
    st = StringIO()
    generate_tojson(s, modulename, st)
    with open(filename_c_tojson, "w") as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()
    st = StringIO()
    generate_fromjson(s, modulename, st)
    with open(filename_c_fromjson, "w") as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    output = TOP_BOILERPLATE.format(datestring=DATESTRING, input_filename=basename)
    output += generate_imports(s["Import"])
    output += msg_ids(s)
    output += msg_names(s)
    output += msg_name_crc_list(s, filename)
    output += typedefs(modulename)
    printfun_types(s["types"], stream, modulename)
    printfun(s["Define"], stream, modulename)
    output += stream.getvalue()
    stream.close()
    output += endianfun(s["types"] + s["Define"], modulename)
    output += calc_size_fun(s["types"] + s["Define"], modulename)
    output += version_tuple(s, basename)
    output += BOTTOM_BOILERPLATE.format(input_filename=basename, file_crc=s["file_crc"])

    return output
